package com.open.exam.utils;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;


/**
 * @author naicha
 * Created on 2017/9/8.
 * 时间格式化工具类
 */
public class DateUtil {

    public static final String dfstr = "yy年mm月dd日";
    public static final String df = "yyyy-MM-dd HH:mm:ss";
    public static final String YEAR_MONTH = "yyyy_MM";
    public static final String YYYYMMDD = "yyyy-MM-dd";
    public static final String YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";

    private static ThreadLocal<SimpleDateFormat> local = new ThreadLocal<>();

    public static String convertEncryptDateToString(Date date){
        SimpleDateFormat sdf = local.get();
        if(sdf == null){
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        local.remove();
        return sdf.format(date);
    }

    public static String convertEncryptDateDayToString(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    public static String dateToString(Date date,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static String convertDateToString(Date date){
        SimpleDateFormat sdf = local.get();
        if(sdf == null){
            sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        }
        local.remove();
        return sdf.format(date);
    }

    public static Date convertStringToDate(String date){

        try {
            SimpleDateFormat sdf = local.get();
            if(sdf == null){
                sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            }
            return sdf.parse(date.replaceAll("/","-"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        local.remove();
        return null;
    }

    public static Date convertStringToDate(String date,String format){
        try {
            if(format == null || format.equals("")){
                format = "yyyy-MM-dd HH:mm:ss";
            }
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(date.replaceAll("/","-"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String convertMillisecondToDateString(long millisecond){
        SimpleDateFormat sdf = local.get();
        if(sdf ==null){
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        local.remove();
        Date date = new Date(millisecond);
        return sdf.format(date);
    }

    /**
     * 取得一天起始时间
     *
     * @param date 日期
     * @return 起始时间
     */
    public static long getDateStartTime(long date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 取得一天结束时间
     *
     * @param date 日期
     * @return 结束时间
     */
    public static long getDateEndTime(long date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    /**
     * 增加/减少日期
     * @param date  时间
     * @param minute  增加/减少的时间
     * @return
     */
    public static Date addOrDelDate(Date date, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
    }

    public static XMLGregorianCalendar dateToXmlDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        DatatypeFactory dtf = null;
        try {
            dtf = DatatypeFactory.newInstance();
        } catch (DatatypeConfigurationException e) {
        }
        XMLGregorianCalendar dateType = dtf.newXMLGregorianCalendar();
        dateType.setYear(cal.get(Calendar.YEAR));
        //由于Calendar.MONTH取值范围为0~11,需要加1
        dateType.setMonth(cal.get(Calendar.MONTH) + 1);
        dateType.setDay(cal.get(Calendar.DAY_OF_MONTH));
        dateType.setHour(cal.get(Calendar.HOUR_OF_DAY));
        dateType.setMinute(cal.get(Calendar.MINUTE));
        dateType.setSecond(cal.get(Calendar.SECOND));
        return dateType;
    }

    /**
     * 获取两个日期的时间差(单位：秒)
     * @param dateOne
     * @param dateTwo
     * @return
     */
    public static Long getTimeDifference (Date dateOne,Date dateTwo){
        if(dateOne == null || dateTwo == null){
            return null;
        }
        // date.getTime()/1000  转换成秒   date.getTime()/1000/60  转换成分
       return  (dateOne.getTime() - dateTwo.getTime()) / 1000;
    }


    public static void main(String[] args) throws ParseException {
        //1533838474967
        long l = System.currentTimeMillis();
        String s = convertMillisecondToDateString(l);
        //17752
        long l1 = TimeUnit.MILLISECONDS.toDays(l);
        String s1 = convertMillisecondToDateString(l1);
        System.out.println(s);
        System.out.println(s1);
    }
}
